package com.ericsson.eritask.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.ericsson.eritask.dao.UserDAO;
import com.ericsson.eritask.dao.UserNameDAO;


@Service
public class MyUserDetailSecurityService implements UserDetailsService {

	
	@Autowired
	UserDAO userDao;
	
	@Override
	public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
		System.out.println(".............................");
		com.ericsson.eritask.domain.User user = userDao.findByUserName(userName);
		//List<GrantedAuthority> authorities = buildUserAuthority(user.getUserRole());
 System.out.println("loadUserByUsername():::"+user.getUserName());
		return buildUserForAuthentication(user, null);
	}

	private  User buildUserForAuthentication(com.ericsson.eritask.domain.User user, List<GrantedAuthority> authorities) {
		System.out.println("buildUserForAuthentication()");
		GrantedAuthority auth =  new SimpleGrantedAuthority("EMPLOYEE");
		authorities = new ArrayList<GrantedAuthority>();
		authorities.add(auth);
		System.out.println(user.getUserName()+" " +user.getPassword());
		return new  User(user.getUserName(), 
				user.getPassword(), authorities);
	}

}
